<template>
	<view>
		<uni-popup ref="popupDialog">
			<view class="popup-bd">
				<view class="popup-title mt20 g3 fw600">完成确认</view>
				<view class="popup-content mt20">
					<view class="g3 f32 lh80 fw500 df ac">
						<view class="title-icon"></view>
						<view
							class="ml20 start"
							v-if="details.scheduleType == 3"
							style="color: red"
							>*</view
						>
						<text class="ml20">{{
							details.scheduleType == 3 ? '提交内容' : '备注'
						}}</text>
					</view>
					<view class="mb20"
						><diy-upload
							limit="2"
							@emitsUrl="getUploadFile($event, tempIndex)"
							:value="fileList"
						></diy-upload
					></view>
					<view class="text-box">
						<view class="tab df lh80">
							<view
								class="flex-1 f32 g3"
								@tap="setCurrent(item.type)"
								:class="current === item.type ? 'active' : ''"
								v-for="(item, index) in textArr"
								:key="index"
							>
								{{ item.name }}
							</view>
						</view>
						<view class="input-h" v-if="current === 'text'">
							<textarea
								v-model="scheduleContent"
								placeholder="日程内容、描述"
								class="pd20"
								placeholder-class="g9"
							></textarea>
						</view>
						<view class="" v-if="current === 'voice'">
							<diy-record
								:context="voiceDTOS.voiceText"
								@input="textinput($event, tempIndex)"
								@upload="getRecordFile($event, tempIndex)"
								:audioUrl="voiceDTOS.voiceUrl"
								:voliceTime="voiceDTOS.voiceDuration"
							></diy-record>
						</view>
					</view>
				</view>
				<view class="popup-bottom df ac">
					<button
						class="primary-button linear w90"
						@tap.stop="$noMultipleClicks(complete, {})"
					>
						{{
							details.scheduleType == 3
								? '确认完成并提交'
								: '确认完成'
						}}
					</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import DiyUpload from '@/subPackagesLog/components/diy-upload/diy-upload.vue'
export default {
	components: {
		DiyUpload,
	},
	props: {
		userList: {
			type: Array,
			default: () => [],
		},
	},
	data() {
		return {
			current: 'voice',
			scheduleId: '',
			scheduleContent: '',
			fileList: [],
			details: {
				id: '',
			},
			noClick: true,
			voiceDTOS: {
				voiceUrl: '',
				voiceDuration: 0,
				voiceText: '',
			},
			textArr: [
				{ name: '文字输入', type: 'text' },
				{ name: '语音输入', type: 'voice' },
			],
		}
	},
	methods: {
		init(data) {
			this.details = data
			this.fileList = []
			this.voiceDTOS = {
				voiceUrl: '',
				voiceDuration: 0,
				voiceText: '',
			}
			this.scheduleContent = ''
			this.$refs.popupDialog.open('bottom')
		},
		// 设置输入方式
		setCurrent(type) {
			this.current = type
		},
		// 图片处理
		getUploadFile(fileList, tempIndex) {
			this.fileList = fileList
		},
		// 文本赋值
		textinput(remark, tempIndex) {
			this.voiceDTOS.voiceText = remark
		},
		//整改项音频
		getRecordFile(volice, tempIndex) {
			this.voiceDTOS.voiceUrl = volice.remark
			this.voiceDTOS.voiceDuration = volice.time
			this.voiceDTOS.voiceText = volice.voiceText
		},
		writeLog() {},
		complete() {
			let params = {
				scheduleId: this.details.id,
				// templateId: this.details.templateId,
				remarkContent: this.scheduleContent,
				voiceDTOS: [],
			}
			if (this.details.scheduleType === 3) {
				if (this.fileList.length === 0) {
					uni.showToast({
						title: '请上传图片',
						icon: 'none',
					})
					return
				}
			}
			if (this.fileList && this.fileList.length > 0) {
				let url = this.fileList.map((item) => {
					return item.url
				})
				params.remarkPicUrl = url
			}
			if (this.voiceDTOS.voiceUrl) {
				params.voiceDTOS.push({
					voiceUrl: this.voiceDTOS.voiceUrl,
					voiceDuration: this.voiceDTOS.voiceDuration,
					voiceText: this.voiceDTOS.voiceText,
				})
			}
			let that = this
			this.$request
				.request('carer-platform/work/log/schedule/finishSchedule', {
					method: 'post',
					data: params,
				})
				.then((res) => {
					if (res.data) {
						uni.showToast({
							title: '操作完成',
							icon: 'none',
						})
						this.$refs.popupDialog.close()
						this.$emit('complete')
					}
				})
		},
	},
}
</script>

<style scoped lang="scss">
.text-box {
	border-radius: 6rpx;
	border: 2rpx solid #f2f2f2;
	.tab {
		align-items: center;
		justify-content: center;
		text-align: center;
		border-radius: 6rpx 6rpx 0 0;
		view {
			border-radius: 6rpx 6rpx 0 0;
			&.active {
				background-color: $primary-color;
				color: #ffffff;
			}
		}
	}
}
/deep/.uni-easyinput__content-textarea {
	height: 270rpx !important;
	padding: 20rpx;
	font-size: 34rpx;
}
.popup-bd {
	height: calc(100vh - 200rpx);
}
</style>
